using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using Aluetjen.EmailService.Contract;

namespace Aluetjen.EMailService.Contract
{
    [DataContract(Name = "MailMessage", Namespace = Namespace.Default)]
    [KnownType(typeof(UTF8Encoding))]
    [KnownType(typeof(DecoderReplacementFallback))]
    [KnownType(typeof(EncoderReplacementFallback))]
    public class MailMessage
    {
        [DataMember]
        public MailAddress Sender { get; set; }
        [DataMember]
        public MailAddress From { get; set; }
        [DataMember]
        public List<MailAddress> ReplyToList { get; set; }
        [DataMember]
        public List<MailAddress> To { get; set; }
        [DataMember]
        public List<MailAddress> Bcc { get; set; }
        [DataMember]
        public List<MailAddress> CC { get; set; }
        [DataMember]
        public string Subject { get; set; }
        [DataMember]
        public string Body { get; set; }
        [DataMember]
        public MailPriority Priority { get; set; }

        [DataMember]
        public Encoding SubjectEncoding { get; set; }
        [DataMember]
        public Encoding BodyEncoding { get; set; }
        [DataMember]
        public Encoding HeadersEncoding { get; set; }

        [DataMember]
        public bool IsBodyHtml { get; set; }

        [DataMember]
        public DeliveryNotificationOptions DeliveryNotificationOptions { get; set; }

        [DataMember]
        public List<Attachment> Attachments { get; set; }

        public MailMessage()
        {
        }

        public MailMessage(string from, string to, string subject, string body, bool isBodyHtml = false, Encoding bodyEncoding = null)
        {
            if (string.IsNullOrEmpty(from)) throw new ArgumentNullException("from");
            if (string.IsNullOrEmpty(to)) throw new ArgumentNullException("from");

            To = to.Split(';').Select(r => r.Trim()).Select(r => new MailAddress {Address = r}).ToList();
            From = new MailAddress {Address = from};
            Subject = subject;
            Body = body;
            BodyEncoding = bodyEncoding ?? Encoding.UTF8;
            IsBodyHtml = isBodyHtml;
        }
    }
}